<?php
//
// Article Class
// The SDMworld! Network
//

uiLoadLibrary('page');

class clsArticle extends cDycmsObject
{
    protected $mClsId = 'Article';

	const allowedTags = '<a><b><em><i><br><font><style>';
	
	function Render(cDycmsClientNode &$r_client, $disp = true)
	{
		if(date('d', $this->mAttributes['date']) == date('d'))
			$date = 'Today '.date('h:ma T',$this->mAttributes['date']);
		else
			$date = date("d M Y h:ma T",$this->mAttributes['date']);
		
		$r_client->AddChild(<<<HTMLEND
		<div class='ContentTitle'>
		<div style='float: right;'>$date</div>
		{$this->mCoreAttributes['title']}
		</div>
HTMLEND
		);
		$r_client->AddChild($cnode = new cHtmlWrapper(
			"<div class='ContentBody'>","</div>\n".
			"<div class='ContentFooter'>".
			"<a href='".dyViewerUrl($this->mClsId, $this->mId)."'>Permalink | Comments</a>".
			"</div>"));

		$cnode->AddChild(new cUiWikiRenderer(
			array('WikiLink'=>'[', 'WikiLinkEnd'=>']'),
			$this->mAttributes['text'], $this->allowedTags));
		
        /*
		if($disp)
		{
			//
			// Show comments
			//

			$q = new cDycmsQuery('articleid:'.$this->mId);
			$q->SetClass('4c5e4ce031e211dba98b0800200c9a66');
			$q->Execute();
			$buf .= $q->GetDisplay();
		};*/
	}

	function DrawEditor(cUiEditor &$editor)
	{
		//
		// Draw the editor form
		//

		$editor->AddChild(<<<END
Type your article text here. You will choose a title for the article
when you publish it.
END
		);
		
		$editor->AddChild(new cUiEditorTextArea('text',
			$this->mAttributes['text'], null));
		
		//
		// Add buttons
		//
		
		$editor->AddChild(new cUiButtonSet(array(
			'name' => '$$os-editorAction',
			'method' => 'POST',
			'action' => ''), $this->mId == 0 ? array(
				'save' => uiGetIcon('disk').'Submit',
				'pub' => uiGetIcon('newspaper_go').'Publish'
			) : array(
				'save' => uiGetIcon('disk').'Save Changes',
				'pub' => uiGetIcon('newspaper_go').'Publish',
				'delete' => uiGetIcon('delete').'Delete',
				'view' => uiGetIcon('magnifier').'View'
		)));
	}
	
	function DrawPublisher(cUiPublisher &$rPublisher)
	{
		$rPublisher->AddChild(new cUiTextBox(array(
			'name'=>'title',
			'value'=>$this->mCoreAttributes['title']
		),'Article Title'));
		
		$rPublisher->AddDefaultButtons();
	}
	
	function GetSummary()
	{
        if($this->mCoreAttributes['title'] === null)
            return 'Unpublished';
        else
            return $this->mCoreAttributes['title'];
	}

	function LoadObject()
	{
        $this->mCoreAttributes = $this->mClass->GetIndexValues($this->mId);
	}

    function Create(){
        $this->mAttributes['text'] = '';
		$this->mAttributes['date'] = time();
    }
}

class cCollectionArticle extends cDycmsAbstractCollection
{
	function GetDisplay()
	{
		$buf = "";
		foreach($this->mObject as $obj)
		{
			$buf .= "<div class='article'>\n";
			$buf .= $obj->GetDisplay(NULL, false);
			$buf .= "</div>\n";
		};
		return $buf;
	}

	public function GetSummary($i = -1)
	{
		if($i > -1)
			return $this->mObject[$i]->GetSummary();

		$vz = $this->GetUniqueValues('site');
		$n = count($vz);
		if($n != 1)
			return "Articles";
		$buf = "/ {$vz[0]}";

		$vz = $this->GetUniqueValues('category');
		$n = count($vz);
		if($n != 1)
			return $buf;
		$buf .= " / {$vz[0]}";
		return $buf;
	}
}

?>
